<!--计量单位-->
<template>
    <div class="content-wrapper a-base-container">
        <div class="big-main-wrapper">
            <div class="btn-wrapper clearfix">
                <a-row class="text-center clearfix">
                    <a-col :xs="24" :sm="24" :md="24" :lg="16" :xl="12">
                        <a-col :span="2" v-if="permissions.includes('unit:add')">
                            <div class="btns" @click="addFun">
                                <a-icon class="btns-icon" type="file-add" />
                                <div class="btns-text">新增</div>
                            </div>
                        </a-col>
                        <a-col :span="2" v-if="permissions.includes('unit:edit')">
                            <div class="btns" @click="edit">
                                <a-icon class="btns-icon" type="edit" />
                                <div class="btns-text">修改</div>
                            </div>
                        </a-col>
                        <a-col :span="2" v-if="permissions.includes('unit:delete')">
                            <div class="btns" @click="remove">
                                <a-icon class="btns-icon" type="delete" />
                                <div class="btns-text">删除</div>
                            </div>
                        </a-col>
                    </a-col>
                </a-row>
            </div>
            <div class="content-tree-wrapper">
                <div class="tree-box" style="padding-top:10px;">
                    <a-tree class="tree" show-line :tree-data="treeData" :default-selected-keys="['0']" :auto-expand-parent="autoExpandParent" @select="treeSelect" :expanded-keys="expandedKeys" @expand="onExpand">
                        <a-icon slot="switcherIcon" type="down" />
                        <template slot="title" slot-scope="{ title }">
                            <span v-if="title.indexOf(searchName) > -1">
                                {{ title.substr(0, title.indexOf(searchName)) }}
                                <span style="color: #f50">{{ searchName }}</span>
                                {{ title.substr(title.indexOf(searchName) + searchName.length) }}
                            </span>
                            <span v-else>{{ title }}</span>
                        </template>
                    </a-tree>
                </div>
                <div class="resize-table-container" :style="parentStyle">
                    <!-- <div class="a-button-bar">
                <a-button type="primary" @click="addFun">新增</a-button>
                <a-button type="primary" @click="edit">修改</a-button>
                <a-button type="primary" @click="remove">删除</a-button>
            </div> -->
                    <a-table :columns="unitColumns" :data-source="data" bordered :pagination="pagination" tableLayout="fixed" style="font-size: 12px;" :row-selection="{selectedRowKeys: selectedKeys, onChange: selection}" @change="onTableChange">
                    </a-table>
                </div>
            </div>

            <drag-modal :title="modalTitle" :visible="untiTypeModal" @cancel="()=>{untiTypeModal = false}" centered width="800px">
                <!-- 计量类型 -->
                <a-row :gutter="24">
                    <a-col :span="4" class="unit-d">分类:</a-col>
                    <a-col :span="14">
                        <a-radio-group v-model="unitType" button-style="solid" @change="add">
                            <a-radio :value="0">单计量</a-radio>
                            <a-radio :value="-1">多计量</a-radio>
                        </a-radio-group>
                    </a-col>
                </a-row>

                <!-- 单计量 -->
                <div v-if="unitType == 0">
                    <a-form class="ant-advanced-search-form" :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 10 }" @submit="saveUnit">
                        <a-row :gutter="24">
                            <a-col :span="20">
                                <a-form-item label="单计量编码">
                                    <a-input v-decorator="['code', {rules: [{ required: true, message: '单计量编码不能为空' }]}]" placeholder="例：01、02、03" />
                                    <span style="float: right;margin-right: -100px;margin-top: 10px;">编码规则：**</span>
                                </a-form-item>
                            </a-col>
                        </a-row>
                        <a-row :gutter="24">
                            <a-col :span="20">
                                <a-form-item label="单计量名称">
                                    <a-input v-decorator="['unitName', {rules: [{ required: true, message: '计量单位名称不能为空' }]}]" placeholder="例：只、个、台、升" />
                                </a-form-item>
                            </a-col>
                        </a-row>
                    </a-form>
                </div>
                <!-- 多计量 -->
                <div v-if="unitType == -1">
                    <a-form class="ant-advanced-search-form" :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 10 }" @submit="handleSubmit">
                        <a-row :gutter="24">
                            <a-col :span="20">
                                <a-form-item label="计量单位组编码">
                                    <a-input v-decorator="['code', {rules: [{ required: true, message: '计量单位组编码不能为空' }]}]" placeholder="例：01、02、03" :disabled="isDisabled" />
                                    <span style="float: right;margin-right: -100px;margin-top: 10px;">编码规则：**</span>
                                </a-form-item>
                            </a-col>
                        </a-row>
                        <a-row :gutter="24">
                            <a-col :span="20">
                                <a-form-item label="计量单位组名称">
                                    <a-input v-decorator="['unitGroupName', {rules: [{ required: true, message: '计量单位组名称不能为空' }]}]" placeholder="例：重量组、面积组、啤酒组" :disabled="isDisabled" />
                                </a-form-item>
                            </a-col>
                        </a-row>
                        <div class="modal-class">
                            <a-table bordered :data-source="editData" :columns="editColumns" :customRow="customClick" :pagination="false" class="border-none margint" :scroll="{x:400,y:200}">
                                <template slot="name" slot-scope="text, record, index">
                                    <!-- <a-textarea auto-size style="margin: -5px 0;border-radius:0;border: 0;" @change="changeRate" v-model="record.name" @click="handleAdd(1, index)" /> -->
                                    <a-input auto-size style="margin: -5px 0;border-radius:0;border: 0;" @change="changeRate" v-model="record.name" @click="handleAdd(1, index)"></a-input>
                                </template>

                                <template slot="changeRate" slot-scope="text, record">
                                    <!-- <a-textarea :disabled="record.isMainUnit" @change="changeRate" auto-size style="margin: -5px 0;border-radius:0;border: 0;" v-model="record.changeRate" /> -->
                                    <a-input :disabled="record.isMainUnit" @keyup="()=>{record.changeRate=record.changeRate.toString().match(/^\d+(?:\.\d{0,4})?/)}" @change="changeRate" auto-size style="" v-model="record.changeRate"></a-input>
                                </template>

                                <template slot="convertType" slot-scope="text, record">
                                    <a-select v-show="record.show" v-model="record.convertType" :disabled="record.isMainUnit" class="select-style">
                                        <a-select-option :value="0">固定</a-select-option>
                                        <a-select-option :value="1">浮动</a-select-option>
                                    </a-select>
                                </template>

                                <template slot="isMainUnit" slot-scope="text, record">
                                    <a-checkbox v-show="record.show" v-model="record.isMainUnit" @change="mainUnitChange(record.key, record.isMainUnit)" />
                                </template>

                                <template slot="isGroup" slot-scope="text, record">
                                    <a-checkbox v-show="record.show" v-model="record.isGroup" />
                                </template>

                                <template slot="operation" slot-scope="text, record, index">
                                    <a-popconfirm icon=" " title="" ok-text="增加行" cancel-text="删除行" @confirm="handleAdd(2)" @cancel="() => onDelete(record.key)">
                                        {{index + 1}}
                                    </a-popconfirm>
                                </template>

                                <template slot="delete" slot-scope="text, record">
                                    <a-popconfirm title="确定要删除?" @confirm="() => onDelete(record.key)"></a-popconfirm>
                                    <a href="javascript:;" @click="onDelete(record.key)">+-</a>
                                </template>
                            </a-table>
                        </div>
                    </a-form>
                    <div class="explain-wrapper">
                        <span>说明：<br>1.建议使用最小的计量单位作为主计量！<br>2.在已被引用的计量单位组中增加新的计量单位，将会在所有涉及的物料档案中增加此计量单位。</span>
                    </div>
                </div>
                <div class="contextmenu-box">
                    <!-- 右键菜单 -->
                    <ul v-show="visibleRC" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
                        <li @click="handleAdd(2)">增加行</li>
                        <li @click="onDelete(thisClickRecord.key)">删除行</li>
                    </ul>

                </div>
                <template slot="footer">
                    <div class="footer-wrapper">
                        <div class="footer-btn">
                            <a-button type="primary" v-if="unitType == 0 && permissions.includes('unit:save')" html-type="submit" @click="saveUnit">保存</a-button>
                            <a-button type="primary" v-if="unitType == -1" html-type="submit" @click="handleSubmit">保存</a-button>
                            <a-button :style="{ marginLeft: '8px' }" @click="() => {untiTypeModal = false}">关闭</a-button>
                        </div>
                    </div>
                </template>
            </drag-modal>
        </div>

    </div>
</template>
<script>
let groupData = [];
let groupMap = new Map();

const treeData = [
    {
        title: "分类",
        key: "",
        children: [
            {
                title: "单计量",
                key: "0",
                scopedSlots: { title: "title" }
            },
            {
                title: "多计量",
                key: "-1",
                scopedSlots: { title: "title" },
                children: groupData
            }
        ]
    }
];

import { mapState } from "vuex";
import { save, pageList, deleteUnit, saveUnit } from "@/api/inventory/unit";
import { unitGroupList } from "@/api/inventory/unitGroup";
import { searchInTreeData } from "@/utils/tree";

export default {
    data () {
        const columns = [
            // { title: "计量单位编码", width: 130, dataIndex: "code"},
            {
                title: "计量单位组名称",
                width: 110,
                dataIndex: "unitGroupId",
                customRender: text => {
                    return groupMap.get(text);
                }
            },
            { title: "计量单位名称", width: 110, dataIndex: "name" },
            {
                title: "主计量",
                width: 90,
                dataIndex: "isMainUnit",
                customRender: (text, row, index) => {
                    if (row.unitGroupId == 0) return;
                    return text ? "是" : "否";
                }
            },
            {
                title: "换算类型",
                width: 80,
                dataIndex: "convertType",
                customRender: (text, row) => {
                    if (row.unitGroupId == 0 || text == null) return;
                    return text == 0 ? "固定" : "浮动";
                }
            },
            { title: "换算率", width: 80, dataIndex: "changeRate" },
            { title: "换算说明", width: 100, dataIndex: "rateDescription" }
        ];

        const singleUnitColumns = [
            { title: "计量单位编码", width: "47%", dataIndex: "code" },
            { title: "计量单位名称", width: "47%", dataIndex: "name" }
        ];

        const editColumns = [
            {
                title: "序号",
                dataIndex: "key",
                align: "center",
                key: "key",
                width: 40,
                customRender: (text, record, index) => {
                    return index + 1;
                }
                // scopedSlots: { customRender: "operation" }
            },
            {
                title: "计量单位名称",
                dataIndex: "name",
                width: 130,
                scopedSlots: { customRender: "name" }
            },
            {
                title: "主计量",
                dataIndex: "isMainUnit",
                width: 50,
                scopedSlots: { customRender: "isMainUnit" },
                align: "center",
            },
            {
                title: "换算类型",
                dataIndex: "convertType",
                scopedSlots: { customRender: "convertType" }
            },
            {
                title: "换算率",
                dataIndex: "changeRate",
                scopedSlots: { customRender: "changeRate" }
            },
            { title: "换算说明", width: 200, dataIndex: "rateDescription" },
            {
                title: "单位组合",
                dataIndex: "isGroup",
                scopedSlots: { customRender: "isGroup" },
                align: "center",
            }
        ];

        return {
            selectedKeys: [],
            selectedData: {},
            selectedIds: [],
            expandedKeys: [],
            autoExpandParent: true,
            searchName: "", //搜索名称
            columns, //查询列表table字段
            unitColumns: singleUnitColumns,
            singleUnitColumns,
            editColumns, //编辑table字段
            treeData, //左边树data
            data: [], //查询列表data
            editData: [], //编辑table data
            unitType: 0,
            tgaUnitType: null,
            editItem: {},
            untiTypeModal: false,
            modalTitle: "新增计量单位",
            scroll: { x: 0, y: 0 },
            parentStyle: "width: 450px",
            count: 0,
            form: this.$form.createForm(this, { name: "advanced_search" }),
            pagination: {
                defaultPageSize: 20,
                current: 1,
                pageSize: 20,
                total: 0,
                size: "small",
                showTotal: total => {
                    return (
                        <div>
                            共 {total} 条数据，每页
                            <a-input-number
                                value={this.pagination.pageSize}
                                min={1}
                                max={500}
                                onPressEnter={e => this.changePageSize(e)}
                            />
                            条
                        </div>
                    );
                },
                showQuickJumper: true,
                showLessItems: true
            },
            postData: {
                //查询条件
                current: 1,
                size: 20,
                unitGroupId: 0
            },
            isMainUnitKey: null, //主计量key



            visibleRC: false,         //控制右键菜单显示
            top: 0,
            left: 0,
            drageBox: null,
            customClick: (record, index) => ({
                on: {
                    contextmenu: (e) => {//右键事件
                        e.preventDefault();

                        let faterBox = document.getElementsByClassName('ant-modal-content')[0];
                        this.drageBox = faterBox.getBoundingClientRect();

                        if (record.number != "合计") {
                            this.openMenu(e);
                            console.log('右键点击的', index);
                            this.thisClickRecord = record;
                        }
                    },
                    click: () => {
                        // console.log(record);
                    }
                },
            }),
            thisClickRecord: {},    //当前右键点击的行的数据
            isDisabled: false,
        };
    },
    created () { },
    mounted () {
        this.PageList();
        this.unitGroupList();
    },
    watch: {
        visibleRC (value) {
            if (value) {
                document.body.addEventListener("click", this.closeMenu);
            } else {
                document.body.removeEventListener("click", this.closeMenu);
            }
        },
    },
    computed: {
        ...mapState({
            userInfo: state => state.user.info,
            permissions: state => state.user.permissions,
        }),
    },
    methods: {
        //新增计量单位
        addFun () {
            this.untiTypeModal = true;
            this.modalTitle = "新增计量单位";
            this.editItem.id = null;
            this.add();
        },
        //获取计量列表
        PageList () {
            const { postData } = this;
            pageList(postData)
                .then(response => {
                    if (response.code === 200) {
                        const { data } = response;
                        const { records, total, current, size } = data;
                        this.data = records;
                        Object.assign(this.pagination, {
                            total
                        });
                    }
                })
                .catch(error => { });
        },
        //获取计量组
        unitGroupList () {
            unitGroupList().then(res => {
                if (res.code == 200) {
                    groupData.splice(0, groupData.length);
                    for (let i = 0; i < res.data.length; i++) {
                        groupData.push({
                            title: "(" + res.data[i].code + ")" + res.data[i].name,
                            key: res.data[i].id,
                            scopedSlots: { title: "title" }
                        });
                        groupMap.set(res.data[i].id, res.data[i].name);
                    }
                    this.expandedKeys = [...groupMap.keys()];
                }
            });
        },
        //新增操作
        add () {
            this.selectedKeys = [];
            this.selectedData = {};
            this.isDisabled = false;
            this.count = 0;
            if (this.unitType == -1) {
                const { count, editData } = this;
                const newData = {
                    key: count,
                    show: true,//新增行数据是否有视图
                    convertType: 0
                };
                this.editData = [newData];
                this.count = count + 1;


                // console.log(this.editData.length)
                // if (this.editData.length < 6) {
                //     let unitCount = this.editData.length;
                //     for(let i=0; i<5 - unitCount; i++) {
                //         this.handleAdd(2);
                //     }
                // }

            }
            this.form = this.$form.createForm(this, {
                mapPropsToFields: () => {
                    return {
                        code: this.$form.createFormField({
                            value: ""
                        }),
                        unitName: this.$form.createFormField({
                            value: ""
                        }),
                    };
                }
            });
        },
        //编辑操作
        edit () {
            this.modalTitle = "修改计量单位";
            this.isDisabled = true;
            if (this.selectedKeys.length == 0) {
                this.selectedData = this.data[0];
            } else {
                if (this.selectedKeys.length != 1) {
                    this.$message.warning("请选择一条数据");
                    return;
                }
            }

            if (this.selectedData.unitGroupId == 0) {
                //单计量
                this.unitType = 0;
                this.editItem.id = this.selectedData.id;
                this.editItem.code = this.selectedData.code;
                this.untiTypeModal = true;
                this.form = this.$form.createForm(this, {
                    mapPropsToFields: () => {
                        return {
                            unitName: this.$form.createFormField({
                                value: this.selectedData.name
                            }),
                            code: this.$form.createFormField({
                                value: this.selectedData.code
                            })
                        };
                    }
                });
            } else {
                //多计量
                this.unitType = -1;
                this.untiTypeModal = true;
                console.log(this.selectedData);
                this.postData.size = 500;
                pageList({
                    current: 1,
                    size: 20,
                    unitGroupId: this.selectedData.unitGroupId
                }).then(response => {
                    if (response.code === 200) {
                        const { data } = response;
                        const { records, total, current, size } = data;
                        this.editData = records;
                        for (let i = 0; i < this.editData.length; i++) {
                            this.editData[i].key = i;
                            this.editData[i].show = true;
                            if (this.editData[i].isMainUnit) {
                                this.isMainUnitKey = i;
                            }
                        }
                        this.count = records.length;
                        this.form = this.$form.createForm(this, {
                            mapPropsToFields: () => {
                                return {
                                    unitGroupName: this.$form.createFormField({
                                        value: groupMap.get(this.selectedData.unitGroupId)
                                    }),
                                    code: this.$form.createFormField({
                                        value: this.selectedData.code
                                    })
                                };
                            }
                        });
                    }
                })
                    .catch(error => { });
            }
        },
        //删除计量单位
        remove () {
            if (this.selectedKeys.length != 1) {
                this.$message.warning("请选择一条数据");
                return;
            }
            let _this = this;
            this.$confirm({
                title: '提示',
                content: '您确定要删除吗？',
                onOk () {
                    deleteUnit(_this.selectedIds).then(res => {
                        if (res.code == 200) {
                            _this.$message.success("删除成功");
                            _this.selectedKeys = [];
                            _this.selectedData = {};
                            _this.unitGroupList();
                            _this.PageList();
                        } else {
                            return // _this.$message.warning(res.message);
                        }
                    });
                },
                onCancel () {
                },
            });
        },
        //单计量保存
        saveUnit (e) {
            e.preventDefault();
            this.form.validateFields((err, values) => {
                if (!err) {
                    console.log("------保存单计量22-------");
                    let code = values.code;
                    let numReg = /^[0-9]*$/
                    let numRe = new RegExp(numReg)
                    if (code.length != 2 || !numRe.test(code)) {
                        this.$message.warning("编码只能是两位数字");
                        return;
                    }

                    this.editItem.name = values.unitName;
                    this.editItem.code = code;
                    saveUnit(this.editItem).then(response => {
                        if (response.code === 200) {
                            this.$message.success("保存成功");
                            //刷新列表
                            this.untiTypeModal = false;
                            this.PageList();
                            this.unitGroupList();
                            this.selectedKeys = [];
                            this.selectedData = {};
                        } else {
                            // this.$message.warning(response.message);
                        }
                    });
                }
            });
        },
        //多计量保存
        handleSubmit (e) {
            e.preventDefault();
            this.form.validateFields((err, values) => {
                if (!err) {
                    console.log("------保存多计量-------");

                    if (this.editData.length < 2) {
                        this.$message.warning("至少一个主计量和辅计量");
                        return;
                    }

                    this.editData = this.editData.filter(item => item.show);
                    let code = values.code;
                    let numReg = /^[0-9]*$/
                    let numRe = new RegExp(numReg)
                    if (code.length != 2 || !numRe.test(code)) {
                        this.$message.warning("编码只能是两位数字");
                        return;
                    }


                    let isMailFlag = false;
                    let isConvertType = false;
                    let unitData = [];
                    let convertType = this.editData[0].convertType;
                    for (let i = 0; i < this.editData.length; i++) {

                        if (!this.editData[i].show) {
                            break;
                        }
                        if (!this.editData[i].name) {
                            this.$message.warning("第" + (i + 1) + "行的计量名不能为空");
                            return;
                        }

                        if (this.editData[i].changeRate) {
                            this.editData[i].changeRate = Number(this.editData[i].changeRate)
                            console.log(this.editData[i].changeRate);
                        }

                        if (!this.editData[i].changeRate) {
                            this.$message.warning("第" + (i + 1) + "行的换算率不能为空,不能为0");
                            return;
                        } else {
                            if (this.editData[i].changeRate <= 0) {
                                this.$message.warning("换算率必须大于0");
                                return;
                            }
                            let rateReg = /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,4})?$/
                            let rateRe = new RegExp(rateReg)
                            if (!rateRe.test(this.editData[i].changeRate)) {
                                this.$message.warning("换算率只能是正小数(小数位4)");
                                return;
                            }
                        }

                        if (this.editData[i].isMainUnit) { //判断是否有主计量
                            isMailFlag = true;
                        } else {
                            if (i + 1 < this.editData.length) {
                                if (!this.editData[i + 1].isMainUnit && this.editData[i].convertType != this.editData[i + 1].convertType) { //判断换算类型是否一致
                                    this.$message.warning("不允许有两种换算类型");
                                    return;
                                }
                            }
                        }

                        if (this.editData[i].convertType == 1) {
                            isConvertType = true;
                        }

                        if (this.selectedKeys.length > 0) {
                            this.editData[i].unitGroupId = this.selectedData.unitGroupId;
                        }

                        this.editData[i].unitGroupName = values.unitGroupName;
                        this.editData[i].code = code;
                        this.editData[i].createDate = null;
                        this.editData[i].updateDate = null;
                        if (this.editData[i].name) {
                            unitData.push(this.editData[i]);
                        }
                    }

                    if (!isMailFlag) {
                        this.$message.warning("至少一个主计量");
                        return;
                    }
                    if (isConvertType && this.editData.length > 2) {
                        this.$message.warning("浮动换算率，只支持1个主计量1个辅计量！");
                        return;
                    }

                    if (unitData.length <= 1) {
                        this.$message.warning("至少一个主计量和辅计量");
                        return;
                    }
                    save(unitData).then(response => {
                        if (response.code === 200) {
                            this.$message.success("保存成功");
                            //刷新 列表
                            this.untiTypeModal = false;
                            this.PageList();
                            this.unitGroupList();
                            this.selectedKeys = [];
                            this.selectedData = {};
                            setTimeout(() => {
                                if (response.message != null && response.message != '') {
                                    this.$confirm({
                                        title: '提示',
                                        content: `计量单位：“${response.message}” 已经使用，不能修改`,
                                        okText: '确认',
                                        // okType: 'danger',
                                        onOk () {
                                            console.log('OK');
                                        },
                                        onCancel () {
                                            console.log('Cancel');
                                        },
                                        class: 'test',
                                    });
                                }
                            }, 500)
                        } else {
                            // this.$message.warning(response.message);
                        }
                    });
                }
            });
        },
        //删除行
        onDelete (key) {
            if (this.editData.length < 2) {
                return;
            }
            let _this = this;
            this.$confirm({
                title: '提示',
                content: '您确定要删除吗？',
                onOk () {
                    const dataSource = [..._this.editData];
                    _this.editData = dataSource.filter(item => item.key !== key);
                },
                onCancel () {
                },
            });
        },
        //增加行
        handleAdd (type, index) {

            console.log("index=", index, this.editData);

            if (type == 1) {
                if (index + 1 != this.editData.length) {
                    return;
                }
            }

            for (let i = 0; i < this.editData.length; i++) {
                if (i != this.editData.length) {
                    this.editData[i].show = true;
                }
            }

            const { count, editData } = this;
            const newData = {
                key: count,
                convertType: 0
            };
            this.editData = [...editData, newData];
            this.count = count + 1;


            console.log(this.editData);
        },
        //主计量切换事件
        mainUnitChange (key, isMainUnit) {
            console.log(this.editData, key);
            if (isMainUnit) {
                this.isMainUnitKey = key;
                this.editData[key].changeRate = "1";
                this.editData[key].convertType = null;
                this.editData[key].rateDescription = "";
                for (let i = 0; i < this.editData.length; i++) {
                    if (key != i) {
                        let data = this.editData[i];
                        if (data.isMainUnit) {
                            data.convertType = 0;
                            data.isMainUnit = false;
                        }

                        if (data.name && data.changeRate && this.editData[key].name) {
                            data.rateDescription =
                                "1" +
                                data.name +
                                "=" +
                                data.changeRate +
                                this.editData[key].name;
                        }
                        this.$set(this.editData, i, data);
                    }
                }
            } else {
                this.isMainUnitKey = null;
            }
        },
        //换算率事件
        changeRate () {
            if (this.isMainUnitKey != null) {
                for (let i = 0; i < this.editData.length; i++) {
                    if (!this.editData[i].isMainUnit) {
                        let data = this.editData[i];
                        data.isMainUnit = false;
                        if (data.name && data.changeRate && this.editData[this.isMainUnitKey].name) {
                            console.log('data.changeRate', data.changeRate)
                            let number = _.clone(data.changeRate).toString();
                            let numberTemp = '';
                            if (number.indexOf('.') != -1) {
                                let numarray = number.split('.');
                                if (numarray[1] && numarray[1].length > 4) {
                                    numberTemp = numarray[0] + '.' + numarray[1].slice(0, 4);
                                } else {
                                    numberTemp = number;
                                }
                            } else {
                                numberTemp = number;
                            }

                            data.rateDescription = "1" + data.name + "=" + numberTemp + this.editData[this.isMainUnitKey].name;
                        }
                        this.$set(this.editData, i, data);
                    }
                }
            }
        },
        //tree 搜索
        treeSearch () {
            const value = this.searchName;
            console.log(value);
            const keys = [];
            const treeData = searchInTreeData(keys, this.treeData, "title", value);
            console.log(keys.length);
            if (keys.length < 1) {
                this.expandedKeys = [...groupMap.keys()];
                this.autoExpandParent = false;
            } else {
                this.expandedKeys = keys;
                this.autoExpandParent = true;
            }
        },
        //tree 展开事件
        onExpand (expandedKeys) {
            this.expandedKeys = expandedKeys;
            this.autoExpandParent = false;
        },
        //tree 单击选择事件
        treeSelect (key, e) {
            console.log(key[0], e);
            if (Number(key[0]) == 0) {
                this.unitType = Number(key[0]);
            } else {
                this.unitType = -1;
            }
            this.selectedKeys = [];
            if (key[0]) {
                if (key[0] == 0) {
                    this.unitColumns = this.singleUnitColumns;
                    this.scroll = { x: 0, y: 0 };
                    this.parentStyle = "width: 450px";
                } else {
                    this.parentStyle = "width: 800px";
                    this.scroll = { x: 1200, y: 500 };
                    this.unitColumns = this.columns;
                }
                this.postData.unitGroupId = key[0];
                this.PageList();
            }
        },
        selection (keys, row) {
            this.selectedKeys = [];
            this.selectedKeys = keys;
            this.selectedData = row[0];
            const ids = new Array();
            row.forEach(function (val) {
                ids.push(val.id);
            });
            this.selectedIds = ids;
            console.log("rowKeys", this.selectedKeys);
        },
        //分页改变
        changePageSize (e) {
            const value = e.target.value;
            const { pagination } = this;

            console.log(e, value);
            Object.assign(this.postData, {
                size: value
            });
            Object.assign(this.pagination, {
                pageSize: Number.parseInt(value)
            });
            this.PageList();
        },
        onTableChange (pagination, filters, sorter) {
            Object.assign(this.postData, {
                current: pagination.current,
                size: pagination.pageSize
            });
            Object.assign(this.pagination, {
                current: pagination.current,
                pageSize: pagination.pageSize
            });
            this.PageList();
        },
        //关闭弹窗回调
        typeModalCancel () {
            console.log("======");
            this.unitType = null;
        },
        openMenu (e) {
            //显示右键菜单
            let box = this.drageBox;
            var x = e.pageX - box.x;
            var y = e.pageY - box.y;
            this.top = y;
            this.left = x;
            this.visibleRC = true;
        },
        closeMenu () {
            //隐藏右键菜单
            this.visibleRC = false;
        },
    }
};
</script>
<style lang="scss" scoped>
    .form-inline label {
        justify-content: left !important;
    }
    .form-inline > .col {
        margin-bottom: 1.5rem !important;
    }
    .icons-list >>> .anticon {
        margin-right: 8px;
        font-size: 16px;
    }
    .ant-advanced-search-form {
        padding: 24px;
        background: #fbfbfb;
    }

    .ant-advanced-search-form .ant-form-item {
        display: flex;
        margin-bottom: 0px !important;
    }

    .ant-advanced-search-form .ant-form-item-control-wrapper {
        flex: 1;
    }

    #components-form-demo-advanced-search .ant-form {
        max-width: none;
    }
    #components-form-demo-advanced-search .search-result-list {
        margin-top: 16px;
        background-color: #fafafa;
        min-height: 200px;
        text-align: center;
        padding-top: 80px;
    }
    .unit-d {
        font-weight: 700;
        font-size: 14px;
        text-align: right;
        color: rgba(0, 0, 0, 0.85);
        padding-right: 28px !important;
    }
    .ant-radio-wrapper {
        font-weight: normal !important;
    }
    .footer-wrapper {
        height: 60px;
        .footer-btn {
            padding-top: 20px;
        }
    }
    .explain-wrapper {
        text-align: left;
        padding-left: 23px;
        width: 90%;
    }
    .select-style {
        .ant-select-selection {
            border: none;
        }
    }
    .margint {
        margin-top: 10px;
    }
    //右键
    .contextmenu-box {
        .contextmenu {
            margin: 0;
            background: #fff;
            z-index: 3000;
            position: absolute;
            list-style-type: none;
            padding: 5px 0;
            border-radius: 4px;
            font-size: 12px;
            font-weight: 400;
            color: #333;
            box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
        }

        .contextmenu li {
            margin: 0;
            padding: 7px 16px;
            cursor: pointer;
        }

        .contextmenu li:hover {
            background: #eee;
        }
    }
</style>
<style lang="less">
    @import '~@/style/setting/inventory.less';
    .modal-class {
        .ant-table-body {
            height: 200px;
        }
    }
</style>